package com.demo.webapp.ui;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.context.ApplicationContextException;
import org.springframework.web.context.support.WebApplicationObjectSupport;

import com.demo.webapp.ui.RepositoryBuilder;

/**
 * 为组件(Component)的配置进行读取，创建组件的容器仓库。 
 * 依赖SpringIoC容器。
 * 提供组件的数据源功能。使用xml配置文件
 * 
 * @see net.sf.navigator.menu.MenuLoader
 * @author ryuu.kk
 * 
 */
public class ComponentLoader extends WebApplicationObjectSupport implements BeanClassLoaderAware {
	/** log日志 **/
	protected final transient Log log = LogFactory.getLog(super.getClass());
	/** 类读取器 **/
	private ClassLoader classLoader;
	/** 仓库创建器 **/
	private RepositoryBuilder builder;
	
	/**
	 * 初始化Component仓库。
	 * @throws ApplicationContextException 异常
	 */
	protected void initApplicationContext() throws ApplicationContextException {
		try {
			if (log.isDebugEnabled()) {
				log.debug("Starting struts-menu initialization");
			}
			builder.setServletContext(getServletContext());
			builder.setBeanClassLoader(classLoader);
			builder.load();
			
		} catch (Exception ex) {
			throw new ApplicationContextException("Failed to initialize Struts Menu repository", ex);
		}
	}
	public void setBuilder(RepositoryBuilder builder) {
		this.builder = builder;
	}

	@Override
	public void setBeanClassLoader(ClassLoader classLoader) {
		this.classLoader = classLoader;
	}
}
